﻿using System.IO;
using System.IO.IsolatedStorage;

namespace Store.WP7.Framework.Settings
{
    public class AppSettings
    {
        public int CustomerId { get; set; }

        public static AppSettings Load()
        {
            AppSettings appSettings = new AppSettings();

            using (var isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!isolatedStorageFile.FileExists("settings.dat"))
                {
                    appSettings.CustomerId = 1;
                    appSettings.Save();
                }
                else
                {
                    using (var stream = isolatedStorageFile.OpenFile("settings.dat", FileMode.Open, FileAccess.Read))
                    {
                        using (var binaryReader = new BinaryReader(stream))
                        {
                            int customerId = binaryReader.ReadInt32();
                            appSettings.CustomerId = customerId;
                        }
                    }
                }
            }

            return appSettings;
        }

        public void Save()
        {
            using (var isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var stream = isolatedStorageFile.OpenFile("settings.dat", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
                {
                    using (var binaryWriter = new BinaryWriter(stream))
                    {
                        binaryWriter.Write(CustomerId);
                    }
                }
            }
        }

        
    }
}
